package com.yx.yinshanjinniu.view;

import android.text.TextUtils;
import android.view.Gravity;

import com.yx.yinshanjinniu.application.MyApplication;
import com.yx.yinshanjinniu.base.BaseActivity;
import com.yx.yinshanjinniu.bean.FollowOrderBean;
import com.yx.yinshanjinniu.bean.UserInfoBean;
import com.yx.yinshanjinniu.utils.DataUtils;
import com.yx.yinshanjinniu.utils.ToastUtil;
import com.zc.mychart.model.GoodsAllBean;


public class CreateOrderModule {

    private BaseActivity mBaseActivity;
    private GoodsAllBean mGroupBean;
    private OrderCreateDialog mOrderCreateDialog;
    private static CreateOrderModule mCreateOrderModule = null;

    private CreateOrderModule(){}

    public static CreateOrderModule getInstance() {
        if (mCreateOrderModule == null) {
            synchronized (CreateOrderModule.class) {
                if (mCreateOrderModule == null) {
                    mCreateOrderModule = new CreateOrderModule();
                }
            }
        }
        return mCreateOrderModule;
    }

    /**
     * 直接创建订单
     * @param activity
     * @param tradeType 订购类型
     * @param followId 跟单id
     * @param bean 合约
     * @param unitPrice 单位
     */
    public void createOrder(BaseActivity activity, int tradeType, String followId, GoodsAllBean bean, double unitPrice, String followUserId, int isFirst) {
        this.mBaseActivity = activity;
        this.mGroupBean = bean;
        if (!DataUtils.isLogin(activity)) {
            activity.onLogoutStatus();
        } else {
            getAccountInfo(tradeType, followId, unitPrice, followUserId, isFirst);
        }
    }

    /**
     * 创建订单风险提示
     * @param activity
     * @param tradeType
     * @param followId
     * @param bean
     * @param unitPrice
     */
    public void createOrderExplain(BaseActivity activity, int tradeType, String followId, GoodsAllBean bean, double unitPrice, String followUserId, int isFirst) {
        this.mBaseActivity = activity;
        this.mGroupBean = bean;
        if (!DataUtils.isLogin(activity)) {
            activity.onLogoutStatus();
        } else {
            if (DataUtils.getBooleanSPByTag(activity, DataUtils.SP_FOLLOW_ORDER_EXPLAIN) || TextUtils.isEmpty(followId)) {
                getAccountInfo(tradeType, followId, unitPrice, followUserId, isFirst);
            } else {
                FollowOrderExplainDialog dialog = new FollowOrderExplainDialog(mBaseActivity);
                dialog.show();
            }
        }
    }

    /**
     *
     * @param tradeType
     * @param followId
     * @param unitPrice
     */
    private void getAccountInfo(final int tradeType, final String followId, final double unitPrice, String followUserId, int isFirst) {
        UserInfoBean userInfo = DataUtils.getUserInfo(mBaseActivity);
        if (mGroupBean != null && userInfo != null) {
            showCreateOrderDialog(tradeType, mGroupBean, userInfo, followId, unitPrice, followUserId, isFirst);
            mBaseActivity.cancelLoading();
        }
    }

    /**
     * 展示创建订单弹窗
     * @param tradeType 下单类型
     * @param group 当前合约
     * @param account 用户信息
     * @param followId 跟单id
     * @param unitPrice 商品单价
     */
    private void showCreateOrderDialog(int tradeType, GoodsAllBean group, UserInfoBean account, String followId, double unitPrice, String followUserId, int isFirst) {
        if (group.during_type == 0){
            ToastUtil.showToast(mBaseActivity, "当前已闭市！");
            return;
        }
        mOrderCreateDialog = new OrderCreateDialog(mBaseActivity, false, Gravity.BOTTOM);
        mOrderCreateDialog.setCreateOrderDialogListener(new OrderCreateDialog.CreateOrderDialogListener() {
            @Override
            public void onDialogLogout() {
                mBaseActivity.onLogoutStatus();
            }

            @Override
            public void onLoading() {
                mBaseActivity.loading();
            }

            @Override
            public void onCancelLoading() {
                mBaseActivity.cancelLoading();
            }

            @Override
            public void onCreateOrderSuccess() {
                MyApplication.getApplication().updateUserDetail();
            }

            @Override
            public void onDismiss() {
            }

            @Override
            public void onShow() {
            }
        });
        mOrderCreateDialog.setFollowId(followId);
        mOrderCreateDialog.setmFollowUserId(followUserId);
        mOrderCreateDialog.setmIsFirst(isFirst);
        mOrderCreateDialog.setUnitPrice(unitPrice);
        mOrderCreateDialog.init(group, account, tradeType);
        mOrderCreateDialog.show();
    }

    public OrderCreateDialog getOrderCreateDialog() {
        return mOrderCreateDialog;
    }

    public void setNewPrice() {
        if (null != mOrderCreateDialog) {
            mOrderCreateDialog.setNewPrice();
        }
    }

    public void setSellPrice(FollowOrderBean bean) {
        if (mSellDialog != null && mSellDialog.isShowing()) {
            mSellDialog.setProfitLoss(bean);
        }
    }

    private OrderFollowSellDialog mSellDialog;

    public void sell(final BaseActivity activity, final FollowOrderBean bean, OrderModuleSellListener listener) {
        this.mSellListener = listener;
        mSellDialog = new OrderFollowSellDialog(activity, bean);
        mSellDialog.setClickListener(new OrderFollowSellDialog.ClickListener() {
            @Override
            public void onConfirm() {
                doSellNetWork(activity, bean);
            }

            @Override
            public void onCancel() {

            }
        });
        if (!mSellDialog.isShowing()) {
            mSellDialog.show();
        }
    }

    private void doSellNetWork(final BaseActivity baseActivity, FollowOrderBean bean) {
        baseActivity.loading();
//        baseActivity.mAppAction.sellOrder(bean.order_id, new ActionCallbackListener<Void>() {
//            @Override
//            public void onSuccess(Void data) {
//                baseActivity.cancelLoading();
//                mSellListener.onSellSuccess();
//                mSellDialog.dismiss();
//            }
//
//            @Override
//            public void onFailure(String errorCode, String message) {
//                baseActivity.cancelLoading();
//                ToastUtil.showToast(baseActivity, message);
//            }
//
//            @Override
//            public void onLogout(String errorCode, String message) {
//                baseActivity.cancelLoading();
//                baseActivity.onLogoutStatus();
//            }
//        });
    }

    private OrderModuleSellListener mSellListener;

    public interface OrderModuleSellListener {
        void onSellSuccess();
    }

}
